<?php

namespace app\admin\controller;

use think\Controller;
use think\Exception;
use think\Request;
use think\Db;
use think\Validate;
use app\admin\controller\Base;
class WebUser extends Base
{
    /**
     * 显示资源列表   
     *
     * @return \think\Response
     */
    public function index()
    {
        if ( empty(input('get.tel')) ) {
            $where = "";

        } else {
            $tel = input('get.tel');
//            $where = "'tel','like','%".$tel."%'";
            $where['tel'] = array('like',"%".$tel."%");

        }

        $data = Db::name('web_users')
            ->field('id, nickname, tel, status')
            ->where($where)
            ->paginate(6, false, ['query'=>request()->param()]);


        $this->assign('data', $data);

        return $this->fetch('WebUser/webuser');

    }


    /**
     * 后台用户查询
     * @return mixed
     */
    public function searchUser ()
    {
        $tel = input('post.')['tel'];

        // 查询条件
        $data = Db::name('web_users')
            ->field('id, nickname, tel, status')
            ->where('tel','like','%'.$tel.'%')
            ->paginate(6);

        $page = $data->render();
        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch('WebUser/webuser');

    }


    /**
     * 显示添加用户资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('WebUser/addWebUser');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = input('post.');

        // 手机正则: |regex:/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/

        // 信息验证
        $rule = [
            'nickname' => 'require|regex:/^[\x{4e00}-\x{9fa5}A-Za-z0-9]{2,16}$/u',
            'tel' => "require",
            'pass' => 'require|max:16',
            'repass' => 'require|confirm:pass'
        ];

        $msg = [
            'nickname.require' => '昵称不能为空,请重试...',
            'nickname.regex' => '昵称必须由中文,字母,数字组成,请重试...',
            'tel.require' => '手机号码不能为空,请重试...',
            // 'tel.regex' => '手机号码必须符合规范,请重试...',
            'pass.require' => '密码不能为空,请重试...',
            'pass.max' => '密码最多只能16位,请重试...',
            'repass.require' => '二次密码不能为空,请重试...',
            'repass.confirm' => '两次密码不一致,请重试...',

        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }

        // 删除二次密码
        unset($data['repass']);
        // 密码加密
        $data['pass'] = sha1($data['pass']);


        // 开启事务
        Db::startTrans();
        try{
            // 插入数据库
            $res = Db::name('web_users')
                ->data($data)
                ->insert();

            if ( $res>0 ){
                Db::commit();
                 $this->success('添加成功!!!', '/webUser');
            }
        }catch(Exception $exception){
            Db::rollback();
            $this->error('新增失败, 请重试...');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        // 查询信息
        $data = Db::name('web_users')
            ->field('nickname, name, tel, email, status')
            ->where('id', $id)
            ->find();

        return view('WebUser/editUser', [
            'data' => $data
        ]);

    }


    public function ajaxSta ()
    {
        $id = input('get.id');
        $sta = input('get.sta');

        $sta == 1? $sta=2:$sta=1;

        $data['status'] = $sta;

        // 开启事务
        Db::startTrans();
        try{
            // 改变用户状态
            $res = Db::name('web_users')
                ->where('id', $id)
                ->update($data);

            if ( $res>0 ){
                Db::commit();
                return 200;
            }
        }catch(Exception $exception){
            Db::rollback();
            return 400;
        }



    }

}
